package com.cardbaobao.cardbabyclient.ui.activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.fragment.base.BaseFragment;
import com.cardbaobao.cardbabyclient.ui.fragment.base.MyFragmentPagerAdapter;
import com.cardbaobao.cardbabyclient.ui.fragment.transactionrecord.DebitRecordFragment;
import com.cardbaobao.cardbabyclient.ui.fragment.transactionrecord.WithdrawRecordFragment;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Jiangzx on 2017/9/11 19:49.
 */

public class TransactionRecordActivity extends BaseActivity {
    public static final int DEBIT_RECORD=0;//收款记录
    public static final int WITHDRAW_RECORD=1;//提现记录
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tv_debit)
    TextView tvDebit;
    @BindView(R.id.divider_debit)
    View dividerDebit;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.divider_withdraw)
    View dividerWithdraw;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    List<BaseFragment> fragments;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_transaction_record;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        fragments=new ArrayList<>();
        DebitRecordFragment debitRecordFragment=new DebitRecordFragment();
        debitRecordFragment.setBuriedInfo(mPrePageId);
        fragments.add(debitRecordFragment);
        WithdrawRecordFragment withdrawRecordFragment=new WithdrawRecordFragment();
        withdrawRecordFragment.setBuriedInfo(mPrePageId);
        fragments.add(withdrawRecordFragment);
        MyFragmentPagerAdapter adapter=
                new MyFragmentPagerAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(DEBIT_RECORD);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTitleNavigator(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        setTitleNavigator(DEBIT_RECORD);
    }



    @OnClick({R.id.ll_debit_record,R.id.ll_withdraw_record})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.ll_debit_record:
                viewPager.setCurrentItem(DEBIT_RECORD);
                break;
            case R.id.ll_withdraw_record:
                viewPager.setCurrentItem(WITHDRAW_RECORD);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(viewPager.getCurrentItem()==DEBIT_RECORD){
            GoogleAnalyticsUtil.setScreenName("交易记录-收款记录");
        }else{
            GoogleAnalyticsUtil.setScreenName("交易记录-提现记录");
        }
    }

    private void setTitleNavigator(int position){
        if(position==DEBIT_RECORD){
            tvDebit.setAlpha(1.0f);
            dividerDebit.setVisibility(View.VISIBLE);
            tvWithdraw.setAlpha(0.5f);
            dividerWithdraw.setVisibility(View.INVISIBLE);
            GoogleAnalyticsUtil.setScreenName("交易记录-收款记录");
        }else{
            tvDebit.setAlpha(0.5f);
            dividerDebit.setVisibility(View.INVISIBLE);
            tvWithdraw.setAlpha(1.0f);
            dividerWithdraw.setVisibility(View.VISIBLE);
            GoogleAnalyticsUtil.setScreenName("交易记录-提现记录");
        }
    }

    @Override
    protected void onDestroy() {
        if(viewPager.getCurrentItem()==DEBIT_RECORD){
            mCurrentPageId=Self.Page.app_debit_transaction_record_page;
        }else{
            mCurrentPageId=Self.Page.app_withdraw_transaction_record_page;
        }
        super.onDestroy();
    }
}
